<?php
namespace app\model;

use think\Model;
use think\facade\Db;
class SettingModel extends Model
{
     // 设置表名
    protected $name = 'sh_setting';
    
    // 设置主键
    protected $pk = 'setting_id';

    public static function set2( $code, $key, $value ){

        // global $_W;
        $uniacid = current_uni_account();
        self::delete2( $code, $key );

        $sql = "insert into sh_setting set `code`= '" . $code
            . "',`uniacid`='" . $uniacid
            . "',`key`='" . $key
            . "',`serialized`='0"
            . "',`value`='" . $value . "'";

        // return pdo_query( $sql );
        return Db::execute( $sql );
    }

    public static function delete2( $code, $key ){

        // global $_W;
        $uniacid = current_uni_account();
        $sql = "delete from sh_setting where `code`='". $code
            ."' and `key`='". $key
            ."' and `uniacid`='". $uniacid ."' "
        ;

        // return pdo_query( $sql );
        return Db::execute( $sql );
    }

    public static function get( $code, $key ){

        // global $_W;
        $uniacid = current_uni_account();
        $sql = "select * from sh_setting where `code`='". $code
            ."' and `key`='". $key
            ."' and `uniacid`='". $uniacid ."' "
        ;

        // $result = pdo_fetch( $sql );
        $result = Db::query( $sql )[0];
        return !empty( $result ) ? $result : false;
    }

    public static function get2( $code, $key ){

        // global $_W;
        $uniacid = current_uni_account();
        $sql = "select * from sh_setting where `code`='". $code
            ."' and `key`='". $key
            ."' and `uniacid`='". $uniacid ."' "
        ;
        $result = pdo_fetch( $sql );
        return $result;
        // $result = Db::query( $sql )[0];
        // return !empty( $result ) ? $result : false;
    }

    public static function getByCode( $code ){

        // global $_W;
        $uniacid = current_uni_account();
        $sql = "select * from sh_setting where `code`='". $code
            ."' and `uniacid`='". $uniacid ."' "
        ;
        // $result = pdo_fetchall( $sql );
        $result = Db::query( $sql );
        return $result;
    }

    public static function get_all(){

        // global $_W;
        $uniacid = current_uni_account();
        $sql = "select * from sh_setting where `uniacid`=" . $uniacid ;

        return pdo_fetchall( $sql );
        // return Db::query( $sql );
    }

    public static function set_all( $data ){
        // global $_W;
        $uniacid = current_uni_account();
        $sql = "INSERT INTO sh_setting (`uniacid`,`code`,`key`,`value`, `serialized`) VALUES ";

        $values = [];
        $codes = [];
        foreach( $data as $val ){

            $str = "'".$uniacid . "','" . $val['code'] ."','" . $val['key'] . "','" . $val['value']."',0";
            $values[] = "(" . $str . ")";

            if( !in_array($val['code'], $codes) ){
                $codes[] =  $val['code'];
            }

        }

        if(!empty( $values ) ){

            foreach( $codes as $del_code ){
                $del_sql = "delete from sh_setting where uniacid= " . $uniacid . " and code = '" . $del_code . "'";
                $ret_del = Db::execute( $del_sql );
            }
            $sql .= implode(',', $values );
            $ret_insert = Db::execute( $sql );

            return $ret_insert;


        }

        return 0;


    }

}